#include "../mqserver/consumer.hpp"
#include <gtest/gtest.h>


mmq::ConsumerManager::ptr cmp;

class ConsumerTest: public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cmp = std::make_shared<mmq::ConsumerManager>();
        cmp->InitQueueConsumer("queue1");
    }
    virtual void TearDown() override
    {
        cmp->Clear();
    }
};

void cb(const std::string tag, const mmq::BasicProperties *bp, const std::string body)
{
    DLOG("消费者 %s 消费了 %s", tag.c_str(), body.c_str());
}

TEST(consumer_test, insert_test)
{
    cmp->Create("Consumer1", "queue1", false, cb);
    cmp->Create("Consumer2", "queue1", false, cb);
    cmp->Create("Consumer3", "queue1", false, cb);
    ASSERT_EQ(cmp->Exists("Consumer1", "queue1"), true);
    ASSERT_EQ(cmp->Exists("Consumer2", "queue1"), true);
    ASSERT_EQ(cmp->Exists("Consumer3", "queue1"), true);
}

TEST(consumer_test, delete_test)
{
    cmp->Remove("Consumer1", "queue1");
    ASSERT_EQ(cmp->Exists("Consumer1", "queue1"), false);
    ASSERT_EQ(cmp->Exists("Consumer2", "queue1"), true);
    ASSERT_EQ(cmp->Exists("Consumer3", "queue1"), true);
}

TEST(consumer_test, choose_test)
{
    mmq::Consumer::ptr cp = cmp->Choose("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->_tag, "Consumer2");

    mmq::Consumer::ptr cp1 = cmp->Choose("queue1");
    ASSERT_NE(cp1.get(), nullptr);
    ASSERT_EQ(cp1->_tag, "Consumer3");

    mmq::Consumer::ptr cp2 = cmp->Choose("queue1");
    ASSERT_NE(cp2.get(), nullptr);
    ASSERT_EQ(cp2->_tag, "Consumer2");
}


int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    int ret = RUN_ALL_TESTS();
}